'''
UNIT INFO
Module for sending email using smtp.
'''

import os
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders


def emailMessage(from_userst, from_passwordst, to_emailst, subjectst,
        bodytekst,smtp_serverst ,attachmentpathst = ''):

    msg = MIMEMultipart()

    msg['From'] = from_userst
    msg['To'] = to_emailst
    msg['Subject'] = subjectst

    msg.attach(MIMEText(bodytekst))

    if attachmentpathst != '':
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(attachmentpathst, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition',
               'attachment; filename="%s"' % os.path.basename(attachmentpathst))
        msg.attach(part)

    mailServer = smtplib.SMTP(smtp_serverst, 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(from_userst, from_passwordst)
    mailServer.sendmail(from_userst, to_emailst, msg.as_string())
    # Should be mailServer.quit(), but that crashes...
    mailServer.close()


if __name__ == '__main__':
    emailMessage('source@y.com','password',
        "target@somedomain.com",
       "------",
       "This is a email sent with python",
       "smtp.somemailserver.com",
       "C:/DoxLap/4temp/flat.txt")

    print 'verzonden!'

